#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Indexes/InterestRateIndex.h>
#pragma unmanaged 
#include <ql\indexes\iborindex.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Indexes {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IIborIndex
	public ref class CIborIndex  : 
            public CInterestRateIndex,
            public Cephei::QL::Indexes::IIborIndex
	{
	protected: 
		boost::shared_ptr<QuantLib::IborIndex>* _ppIborIndex;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::IborIndex>* _phIborIndex;
#endif
		Object^ _IborIndexOwner;     // reference to object that manages the storage for this object
	internal:
		CIborIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h);
        CIborIndex (boost::shared_ptr<QuantLib::IborIndex>& childNative, Object^ owner);
        CIborIndex (QuantLib::IborIndex& childNative, Object^ owner);
        CIborIndex (CIborIndex^ copy);
        CIborIndex (PLATFORM::Type^ t);
#ifdef STRUCT
        CIborIndex (QuantLib::IborIndex childNative);
#endif       
#ifdef HANDLE
		CIborIndex (QuantLib::Handle<QuantLib::IborIndex>& childNative, Object^ owner);
		CIborIndex (QuantLib::Handle<QuantLib::IborIndex> childNative);
#endif
		virtual ~CIborIndex ();
		!CIborIndex ();

	internal:
		QuantLib::IborIndex& GetReference ();
		boost::shared_ptr<QuantLib::IborIndex>& GetShared ();
		QuantLib::IborIndex* GetPointer ();
        void SetIborIndex (boost::shared_ptr<QuantLib::IborIndex> native)
        {
            if (_ppIborIndex != NULL)
                delete _ppIborIndex;
            _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (native);
            SetInterestRateIndex (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppIborIndex));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::IborIndex>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property QL::Times::BusinessDayConventionEnum BusinessDayConvention 
        {
		    virtual QL::Times::BusinessDayConventionEnum get () ;
        }
		virtual Cephei::QL::Indexes::IIborIndex^ Clone (Cephei::QL::Termstructures::IYieldTermStructure^ forwarding) ;
        property Boolean EndOfMonth 
        {
		    virtual Boolean get () ;
        }
		virtual Double ForecastFixing (DateTime fixingDate) ;
		virtual DateTime MaturityDate (DateTime valueDate) ;
        property Cephei::QL::Termstructures::IYieldTermStructure^ ForwardingTermStructure 
        {
		    virtual Cephei::QL::Termstructures::IYieldTermStructure^ get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IIborIndex Vector interface
	ref class CIborIndexVector sealed : 
							    public Core::INativeVector<IIborIndex^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IIborIndex^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::IborIndex>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::IborIndex>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::IborIndex>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CIborIndexVector ();
#ifdef STRUCT
		CIborIndexVector (std::vector<QuantLib::IborIndex>& vec, Object^ owner);
		CIborIndexVector (std::vector<QuantLib::IborIndex> vec);
#endif       
		CIborIndexVector (std::vector<boost::shared_ptr<QuantLib::IborIndex>>& vec, Object^ owner);
#ifdef HANDLE
		CIborIndexVector (std::vector<QuantLib::Handle<QuantLib::IborIndex>>& vec, Object^ owner);
#endif
		virtual ~CIborIndexVector ();
		!CIborIndexVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IIborIndex^ default[int] 
		{
			virtual IIborIndex^ get (int index) = Core::INativeVector<IIborIndex^>::default::get;
			virtual void set (int index, IIborIndex^ value) = Core::INativeVector<IIborIndex^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Indexes::IIborIndex^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IIborIndex^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IIborIndex^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IIborIndex^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IIborIndex^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::IborIndex>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::IborIndex>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::IborIndex>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Indexes::IIborIndex^>::typeid)]
	[FactoryFor(Cephei::QL::Indexes::IIborIndex::typeid)]
	[FactoryFor(Cephei::QL::Indexes::IIborIndex_Factory::typeid)]
	public ref class CIborIndex_Factory sealed : public IIborIndex_Factory
	{
	public:
        virtual IIborIndex^ Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h);
        /* <summary> Create a hybrid Vector of IIborIndex, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IIborIndex^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IIborIndex, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IIborIndex^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IIborIndex^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IIborIndex^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IIborIndex^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IIborIndex^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Indexes::IIborIndex^>^ CreateNativeVector ()
		{
			return REFNEW CIborIndexVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Indexes::IIborIndex^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Indexes::IIborIndex^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Indexes */}
